﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZedGraph;

namespace erse253_homework
{
    public partial class hScatterplot : BaseWindowChart
    {

        double[,] data;
        public hScatterplot(double[,] data)
        {
            InitializeComponent();
            this.data = data;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int hx, hy;
            Cursor.Current = Cursors.WaitCursor;
            if (int.TryParse(textBox1.Text, out hx) && int.TryParse(textBox2.Text, out hy) && ((hx > 0 && hy == 0) || (hy > 0 && hx == 0)))
                {
                    double[,] x;
                    double[,] y;
                    GeneratehScatterplot(out x, out y, hx, hy);
                    base.ClearList();

                    FormatScatterplot(ConvertMatrixToVector(x), ConvertMatrixToVector(y));
                    zedGraphControl1.Refresh();
                }

            Cursor.Current = Cursors.Default;
        }

        private double[] ConvertMatrixToVector(double[,] m) 
        {
            double[] vector = new double[m.GetLength(0)*m.GetLength(1)];
            for (int i = 0; i < m.GetLength(0); i++)
                for (int j = 0; j < m.GetLength(1); j++)
                    vector[i + j * m.GetLength(0)] = m[i, j];

            return vector;
        }

        private void GeneratehScatterplot(out double[,] x, out double[,] y, int hx, int hy)
        {
            SetParameters(hx, hy, out x, out y);

            Chart scatterplot = new Chart();
            scatterplot.hScatterplot(data, x, y, hx, hy);
        }

        private void SetParameters(int hx, int hy, out double[,] x, out double[,] y)
        {
            if (data != null)
            {
                if (hx == 0 && hy == 0)
                {
                    x = new double[data.GetLength(0), data.GetLength(1)];
                    y = new double[data.GetLength(0), data.GetLength(1)];
                }
                else if (hx == 0)
                {
                    x = new double[data.GetLength(0), (data.GetLength(1) - (hy / 2 + 1))];
                    y = new double[data.GetLength(0), (data.GetLength(1) - (hy / 2 + 1))];
                }
                else
                {
                    x = new double[(data.GetLength(0) - (hx / 2 + 1)), data.GetLength(1)];
                    y = new double[(data.GetLength(0) - (hx / 2 + 1)), data.GetLength(1)];
                }
            }
            else 
            {
                x = null;
                y = null;
            }
        }

        private void FormatScatterplot(double[] x, double[] y)
        {
            LineItem myCurve = new LineItem("hScatterplot", x, y, Color.Red, SymbolType.Circle);

            myCurve.Line.IsVisible = false;
            myCurve.Symbol.Size = 3.0F;
            myCurve.Symbol.Fill = new Fill(Color.Red);
            myCurve.Line.Fill.Type = FillType.GradientByZ;

            base.AddItem(myCurve, "hScatterplot", "Value", "Value", "hScatterplot");
        }

    }
}
